package com.xinxin.base.holder;

import java.util.function.Supplier;

/**
 * @author ys
 * @date 2021/4/26
 */
public class ThreadLocalContextHolder<T> {

    private final ThreadLocal<T> contextThreadLocal;

    public ThreadLocalContextHolder() {
        contextThreadLocal = new ThreadLocal<>();
    }

    public ThreadLocalContextHolder(Supplier<T> supplier) {
        contextThreadLocal = ThreadLocal.withInitial(supplier);
    }

    public void setContext(T context) {
        contextThreadLocal.set(context);
    }

    public T getContext() {
        return contextThreadLocal.get();
    }

    public void clean() {
        contextThreadLocal.remove();
    }
}
